<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <!-- <role-search @search="reload" /> -->
      <!-- 数据表格 -->

      <ele-pro-table ref="table"
                     :columns="columns"
                     :datasource="datasource"
                     :selection.sync="selection"
                     height="calc(100vh - 265px)"
                     full-height="calc(100vh - 116px)"
                     tool-class="ele-toolbar-form"
                     row-key="id">
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <!-- <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            批量删除
          </el-button> -->
        </template>

        <template v-slot:feedbackUrl="{ row }">
          <div class="flex-wrap">
            <div v-for="(item, i) in row.feedbackUrl"
                 :key="i"><img @click="onPreview(item)"
                   class="categoryImages"
                   :src="item"
                   alt="" /></div>
          </div>
        </template>
        <template v-slot:content="{ row }">
          <div v-html="row.content"></div>
        </template>
        <template v-slot:notShow="{ row }">
          <el-switch :active-value="1"
                     :inactive-value="2"
                     v-model="row.notShow"
                     @change="editStatus(row)" />
        </template>
        <!-- 状态列 -->

        <template v-slot:discount_money="{ row }">
          <div>
            {{ row.discount_money }}
          </div>
        </template>
        <template v-slot:sort="{ row }">
          <i class="sort-handle el-icon-rank ele-text-secondary"
             style="cursor: move"></i>
          <div v-if="false">{{ row }}</div>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-popconfirm class="ele-action"
                         title="确定要删除改条信息吗？"
                         @confirm="remove(row)">
            <template v-slot:reference>
              <el-link type="danger"
                       :underline="false"
                       icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
      <el-image-viewer v-if="showViewer"
                       style="z-index: 9999"
                       :on-close="closeViewer"
                       :url-list="imgViewArr" />
    </el-card>
    <!-- 编辑弹窗 -->
    <role-edit :data="current"
               :drawerEditBoolen.sync="drawerEditBoolen"
               @done="reload" />
    <!-- 权限分配弹窗 -->
  </div>
</template>

<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer';
import { getToken } from '@/utils/token-util';
import {
  userOssSignature,
  qnyOssSignature
} from '@/api/creatCenter/material';
import axios from 'axios';
import { feedbackList, feedbackDelete } from '@/api/feedback/index.js';
// import RoleSearch from './components/role-search';
import Sortable from 'sortablejs';
import RoleEdit from './components/role-edit';
import { carouselList } from './constant/constant';
import {
  sitecCategoriesDelete,
  siteCategoriesList,
  siteCategoriesModify,
  siteCategoriesDetail,
  siteBatchCategoriesDelete
} from '@/api/serviceManagement/categoriesList';
export default {
  name: 'Recharge',
  components: {
    // RoleSearch,
    RoleEdit,
    ElImageViewer
  },
  data () {
    return {
      carouselList,
      // 表格列配置
      columns: [
        {
          prop: ' feedbackUrl',
          label: '反馈图片',
          sortable: 'custom',
          width: 150,
          align: 'center',
          showOverflowTooltip: false,
          slot: 'feedbackUrl'
        },
        {
          prop: 'feedbackContent',
          label: '反馈内容',
          sortable: 'custom',
          showOverflowTooltip: true
        },
        {
          prop: 'contactInformation',
          label: '联系方式',
          sortable: 'custom',
          showOverflowTooltip: true,
          width: 155,
          align: 'center'
        },

        {
          prop: 'createDate',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          width: 155,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue, 'yyyy-MM-dd HH:mm');
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 230,
          align: 'center',
          resizable: false,
          slot: 'action',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      showViewer: false,
      // 是否显示编辑弹窗
      drawerEditBoolen: false,
      // 是否显示导入弹窗
      showAuth: false,
      showChoose: false,
      // 图片预览数组
      imgViewArr: []
    };
  },
  mounted () {
    this.rowDrop();
  },
  activated () {
    this.reload()
  },
  computed: {},

  methods: {
    // 关闭查看器
    closeViewer () {
      this.showViewer = false;
    },
    rowDrop () {
      const tbody = document.querySelector('.el-table__body-wrapper tbody');
      const _this = this;
      Sortable.create(tbody, {
        forceFallback: true,
        dragClass: 'drag',
        delay: 100,
        animation: 1000,
        onEnd ({ newIndex, oldIndex }) {
          console.log(newIndex);
          console.log(oldIndex);
          //更换表格数据 后续拖动坐标才会正确
          let data = _this.$refs.table.getData();

          let nid = data[newIndex].id;
          let oid = data[oldIndex].id;

          const currRow = data.splice(oldIndex, 1)[0];
          data.splice(newIndex, 0, currRow);

          console.log(data);
          //去数据库更换排序

          console.log(_this.$refs.table.getData());
        }
      });
    },

    /* 表格数据源 */
    async datasource ({ page, limit, where, order }) {
      const res = await feedbackList({
        page,
        limit,
        ...where,
        siteId: this.$store.state.site.info.id
      });

      res.list.forEach((item) => {
        if (item.feedbackUrl) {
          if (item.feedbackUrl.includes(',')) {
            console.log('字符串中包含逗号');
            // 包含的进行slice(',') 分割
            item.feedbackUrl = item.feedbackUrl
              .split(',')
              .map((item) => item.trim());
          } else {
            console.log('字符串中不包含逗号');
            item.feedbackUrl = item.feedbackUrl.split();
          }
        }
      });
      console.log(res.list);
      return res;
    },

    /* 刷新表格 */
    reload (where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 显示编辑 */
    async openEdit (row) {
      console.log(row);
      if (row?.id) {
        const res = await siteCategoriesDetail(row.id);

        this.current = res;
        // 用户余额管理
        this.drawerEditBoolen = !this.drawerEditBoolen;
      } else {
        this.drawerEditBoolen = !this.drawerEditBoolen;
        this.current = null;
      }
    },

    /* 更改状态 */
    editStatus (row) {
      const loading = this.$loading({ lock: true });
      siteCategoriesModify({
        id: row.id,
        notShow: row.notShow,
        siteId: this.$store.state.site.info.id
      })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.status = !row.status ? 1 : 2;
          this.$message.error(e.message);
        });
    },
    // 点击按钮预览图片
    onPreview (imgStr) {
      if (imgStr.length) {
        if (imgStr.includes(',')) {
          console.log('字符串中包含逗号');
          // 包含的进行slice(',') 分割
          this.imgViewArr = imgStr.split(',').map((item) => item.trim());
        } else {
          console.log('字符串中不包含逗号');
          this.imgViewArr = imgStr.split();
        }

        this.showViewer = true;
      } else {
        this.$message({
          message: '没有上传图片无法进行预览',
          type: 'warning'
        });
      }
    },
    /* 显示分配权限 */
    openAuth (row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    remove (row) {
      const loading = this.$loading({ lock: true });
      feedbackDelete(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch () {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      console.log(this.selection);
      this.$confirm('确定要删除选中的信息吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          console.log(this.selection.map((d) => d.id));
          siteBatchCategoriesDelete(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    }
  }
};
</script>

<style lang="less">
.categoryImages {
  width: 50px;
  height: 50px;
  margin-left: 10px;
  cursor: pointer;
}
</style>
